package com.example.gsnd21study.web;


import com.example.gsnd21study.entity.Student;
import com.example.gsnd21study.mapper.StudentMapper;
import com.example.gsnd21study.utils.Result;
import com.example.gsnd21study.utils.ResultCode;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequiredArgsConstructor
@RestController
@RequestMapping("/student")
public class studentController {

    private final StudentMapper studentMapper;


    /**
     * 根据sno查询
     * @param sno
     * @return
     */
    @GetMapping("{sno}")
    public Result<Student> selectById(@PathVariable Long sno) {

        // 手动创建一个系统的运行时异常
        //int i = 1 / 0;

        // 手动抛出一个自定义异常
        // throw new Exception(ResultCode.RESOURCE_NOT_FOUND);
        Student student = studentMapper.selectById(sno);
        return Result.success(student);
    }

    /**
     * 根据条件查询
     * @param name
     * @param age
     * @param gender
     * @return
     */
    @GetMapping("/all")
    public Result<List<Student>> selectAll(@RequestParam String name , @RequestParam Integer age , @RequestParam String gender ) {
        List<Student> students = studentMapper.selectByCondition(name, age, gender);
        return Result.success(students);
    }


    /**
     * 条件更新
     * @param student
     * @return
     */
    @PutMapping
    public Result<?> updateById(@RequestBody Student student) {
        studentMapper.update(student);
        return Result.success("更新成功");
    }

    /**
     * 新增
     * @param student
     * @return
     */
    @PostMapping
    public Result<?> insert(@RequestBody Student student) {
        studentMapper.insert(student);
        return Result.success("添加成功");
    }


    // 传递一个参数和传递多个参数没有什么区别，所以直接传递多个参数
    /**
     * 删除单个数据
     * @param sno
     * @return
     */
    @DeleteMapping("{sno}")
    public Result<?> delete(@PathVariable Long sno) {
        studentMapper.deleteById(sno);
        return Result.success("删除成功");
    }
    /**
     * 批量删除
     * @param snows
     */
    @DeleteMapping("/all")
    public Result<?> deleteBatch(@RequestBody List<Long> snows) {
        studentMapper.deleteBatch(snows);
        return Result.success();
    }
    
}
